package test;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;

import jdclib.io.HybridReader;
import jdclib.io.ParseException;

public class Examples
{
	public static void main(String[] args) throws IOException, ParseException
	{
		/*
		 * Data be from a byte[] and associated stream for simplicities sake in this example.
		 * However, you could use any type of inputstream and associated data source.
		 */
		byte[] data = "Doe,John,33*Blow,Joe,18*".getBytes();
		InputStream stream = new ByteArrayInputStream(data);
		
		/*
		 * Delimiter based reading
		 */
			delimitedReadingExample(stream);
		
		/*
		 * Parseable instance reading
		 */
			//instanceReaderExample(stream);
		
		/*
		 * Convenience method for reading all Parseable instances in the stream
		 */
			//completeInstanceReaderExample(stream);
	}
	
	public static void delimitedReadingExample(InputStream stream) throws IOException
	{
		System.out.println("Delimiter example:");
		HybridReader hr = new HybridReader(stream);
			while(hr.ready())
			{
				byte[] temp = hr.readTo("*",false, true);
				System.out.println("\t"+new String(temp));
			}
		hr.close();
	}
	
	public static void instanceReaderExample(InputStream stream) throws IOException, ParseException
	{
		System.out.println("Instance reading example:");
		HybridReader hr = new HybridReader(stream);
			while(hr.ready())
			{
				Person p = hr.readParseable(new Person());
				System.out.println("\t"+p);
			}
		hr.close();
	}
	
	public static void completeInstanceReaderExample(InputStream stream) throws IOException, ParseException
	{
		System.out.println("Convenience method example:");
		LinkedList<Person> people = HybridReader.readAllParseable(stream, new Person());
		for(Person p : people)
			System.out.println("\t"+p);
	}
}
